package com.alibaba.fastjson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.util.JacksonUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * 临时过渡，请使用Jackson
 */
@Deprecated
public final class JSONObject extends ObjectNode {

    private static final long serialVersionUID = -3925943643694149508L;

//    private ObjectMapper mapper = new ObjectMapper();

    public JSONObject(JsonNodeFactory nc) {
        super(nc);
    }

    public JSONObject() {
        super(JsonNodeFactory.instance);
    }

    public JSONObject(JsonNodeFactory nc, Map<String, JsonNode> kids) {
        super(nc, kids);
    }

    public JSONObject(Map<String, JsonNode> map) {
        super(JsonNodeFactory.instance, map);
    }

    public JSONObject getJSONObject(String key) {
        JsonNode jn = findValue(key);
        Iterator<Map.Entry<String, JsonNode>> iterator = jn.fields();
        Map<String, JsonNode> kids = new HashMap<>();
        for (Iterator<Map.Entry<String, JsonNode>> it = iterator; it.hasNext(); ) {
            Map.Entry<String, JsonNode> entry = it.next();
            kids.put(entry.getKey(), entry.getValue().deepCopy());
        }
        return new JSONObject(JsonNodeFactory.instance, kids);
    }

    public JSONArray getJSONArray(String key) {
        JsonNode jn = findValue(key);
        List<JsonNode> children = new ArrayList<>();
        children.add(jn);
        return new JSONArray(JsonNodeFactory.instance, children);
    }
    
    public <T> T toJavaObject(Class<T> clazz) {
    	//TODO 待实现
    	return null;
    }
    
    public String toJSONString() {
        return toString();
    }
}
